package org.train.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.train.dao.ProjectDao;
import org.train.dao.RevenueExpenditureDao;
import org.train.entity.*;
import org.train.service.ProjectService;
import org.train.util.AmountUtil;
import org.train.util.R;
import org.train.util.RUtil;

import java.util.*;

@Service
public class ProjectService extends ServiceImpl<ProjectDao, ProjectPo>{

    @Autowired
    private ProjectDao pd;

    @Autowired
    private RevenueExpenditureDao red;

    public R queryAllInfos(Integer pageNum, Integer pageSize, String search){
        List<ProjectInfoVo> result = new ArrayList<>();
        List<ProjectPo> projectPoList;
        if(search != null){
            Page<ProjectPo> page = pd.selectPage(new Page<ProjectPo>(pageNum, pageSize, false),
                    new QueryWrapper<ProjectPo>()
                            .eq("deleted", 0)
                            .and(queryWrapper->{
                                queryWrapper.like("contract_name", search).or().like("project_year", search);
                            }));
            projectPoList = page.getRecords();
        }else{
            Page<ProjectPo> page = pd.selectPage(new Page<ProjectPo>(pageNum, pageSize, false),
                    new QueryWrapper<ProjectPo>()
                            .eq("deleted", 0));
            projectPoList = page.getRecords();
        }
        if(projectPoList.size() == 0){
            // 对不不存在满足要求的项目的情况，直接返回空数组，不进行后续的计算
            return RUtil.success(result);
        }
        for(ProjectPo projectPo: projectPoList){
            ProjectInfoVo one = new ProjectInfoVo().assignProjectPo(projectPo);
            List<RevenueExpenditurePo> reps = red.selectList(
                    new QueryWrapper<RevenueExpenditurePo>()
                            .eq("project_id", projectPo.getId())
                            .eq("deleted", 0));

            // 当年的年度采用当前日期的年
            // HACK 不知道getWeekYear是啥年
            int currYear = Calendar.getInstance().getWeekYear();
            AmountUtil.Amounts amounts = AmountUtil.calcAmounts(reps, currYear);
            one.setAnnualBilling(amounts.annualBilling);
            one.setAnnualExpenditure(amounts.annualExpenditure);
            one.setTotalBilling(amounts.totalBilling);
            one.setTotalExpenditure(amounts.totalExpenditure);
            one.setTotalFundsReceived(amounts.totalFundsReceived);

            result.add(one);
        }
        return RUtil.success(result);
    }

    public R queryTotalInfo(){
        ProjectInfoVo result = new ProjectInfoVo();
        List<ProjectPo> projectPoList = pd.selectList(new QueryWrapper<ProjectPo>().eq("deleted", 0));
        if(projectPoList.size() == 0){
            // 对不不存在满足要求的项目的情况，直接返回，不进行后续的计算
            return RUtil.success(result);
        }
        for(ProjectPo projectPo: projectPoList){
            List<RevenueExpenditurePo> reps = red.selectList(
                    new QueryWrapper<RevenueExpenditurePo>()
                            .eq("project_id", projectPo.getId())
                            .eq("deleted", 0));

            int currYear = Calendar.getInstance().getWeekYear();
            AmountUtil.Amounts amounts = AmountUtil.calcAmounts(reps, currYear);
            System.out.println(result);
            result.setAnnualBilling(result.getAnnualBilling() + amounts.annualBilling);
            result.setAnnualExpenditure(result.getAnnualExpenditure() + amounts.annualExpenditure);
            result.setTotalBilling(result.getTotalBilling() + amounts.totalBilling);
            result.setTotalExpenditure(result.getTotalExpenditure() + amounts.totalExpenditure);
            result.setTotalFundsReceived(result.getTotalFundsReceived() + amounts.totalFundsReceived);
            result.setContractAmount(result.getContractAmount() + projectPo.getContractAmount());
        }
        return RUtil.success(result);
    }

    public R queryDetail(Integer id){
        ProjectPo tmp = pd.selectOne(new QueryWrapper<ProjectPo>().eq("id", id));
        ProjectDetailVo result = new ProjectDetailVo().assignProjectPo(tmp);

        List<RevenueExpenditurePo> reps = red.selectList(
                new QueryWrapper<RevenueExpenditurePo>()
                        .eq("project_id", tmp.getId())
                        .eq("deleted", 0));

        int currYear = Calendar.getInstance().getWeekYear();
        AmountUtil.Amounts amounts = AmountUtil.calcAmounts(reps, currYear);
        result.setTotalBilling(amounts.totalBilling);
        result.setTotalFundsReceived(amounts.totalFundsReceived);
        result.setTotalAdvancePayment(amounts.totalAdvancePayment);
        result.setTotalExpenditure(amounts.totalExpenditure);

        return RUtil.success(result);
    }

    public R queryAllBaseInfos(Integer year){
        return RUtil.success(pd.queryAllBaseInfos(year));
    }

    public R saveOrUpdateOne(ProjectPo po){
        this.saveOrUpdate(po);
        return RUtil.success();
    }

    public R removeBatchByUpdate(List<Integer> ids){
        super.update(new UpdateWrapper<ProjectPo>()
                .in("id", ids)
                .set("deleted", 1));
        return RUtil.success();
    }
}
